﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-
# KonomeBot developed by Farseerfc,irolar
#if you would like to join in the develop, please twitter @irolar
#xmpp_handler, provide main founction

from google.appengine.api import xmpp
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from req_handler import *

class XMPPHandler(webapp.RequestHandler):
    def post(self):
        message = xmpp.Message(self.request.POST)
        address=message.sender.encode('utf8').split("/")[0].lower()
        try :
            args = message.body.encode('utf8').strip().split(' ')                          # arguments is splited by space
            for i in range(args.count('')):
                args.remove('')
            cmd_str = 'help'                                                        # default command is help
            cmd = Cmd_help(message,args)
            if len(args) >= 1:
                cmd_str = args[0].strip().lower()                                   # command is given as the first word
            cmd_name = 'Cmd_'+cmd_str
            if cmd_name in  globals() :                                 # search for the Class with name Cmd_($command_name)
                cmd = eval (cmd_name + '(message,args)')    # init a object of that command class, passing message and args
            cmd.reply()                                                                 # let it do the reply
        except Exception, e:
            message.reply( 'Error: %s !' % e )                  # if error, send error message via xmpp
            raise

application = webapp.WSGIApplication([('/_ah/xmpp/message/chat/', XMPPHandler)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
